//引入样式
require('../../assets/css/normalize.css')
require('../../assets/css/reset.css')
require('./mine_sports_data.less');

//引入模块
const dom = require('../../utils/dom.js');
const axios = require('../../utils/axios.js');
import * as echarts from 'echarts';

document.addEventListener('DOMContentLoaded', async function () {
    // 返回上一页
    dom.get('#back').addEventListener("click", function () {
        history.back();
    })
    // 渲染数据
    let res = await axios.get('/api/exercise')

    let { sum7Calorie, sum7Duration, sum7Meter, days } = res.data.data
    console.log(days);
    dom.get('#duration').textContent = sum7Duration;
    dom.get('#calorie').textContent = sum7Calorie;
    dom.get('#meter').textContent = sum7Meter;
    //处理数据
    let x1 = days.map(v => {
        return v.date.slice(5);
    })
    let y1 = days.map(v => {
        return v.sumDuration;
    })
    // 使用echarts绘制图表
    // 基于准备好的dom，初始化echarts实例

    var myChart = echarts.init(dom.get('.chart1'));
    // 绘制柱状图表
    myChart.setOption({
        title: {
            text: '近7天运动时长',
            top: 15,
            left: 20,
        },
        grid: {
            left: 50,
            top: 50,
            height: 100,
        },
        tooltip: {},
        xAxis: {
            type: 'category',
            data: x1
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '销量',
                type: 'bar',
                data: y1,

                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)'
                }
            }
        ],
        color: ['#83a7d1'],
    });

    // 获取数据并赋值
    let data1 = 0;
    days.forEach(v => {
        data1 += v.exerciseData[0].duration;
    });
    let data2 = 0;
    days.forEach(v => {
        data2 += v.exerciseData[1].duration;
    });
    let data3 = 0;
    days.forEach(v => {
        data3 += v.exerciseData[2].duration;
    });
    // 绘制饼图
    var myChart2 = echarts.init(dom.get('.chart2'));
    myChart2.setOption({
        title: {
            text: '运动分类',
            top: 15,
            left: 20,
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            top: 'middle',
            left: 15,
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: '70%',
                left: 40,
                data: [
                    { value: data1, name: '跑步' },
                    { value: data2, name: '骑行' },
                    { value: data3, name: '训练' },
                ],
                color: [
                    '#58bb92', '#736171', '#ee5c43'
                ],
                label: {
                    position: "inside",
                    color: '#fff',
                    formatter: `{d}%`,
                    fontSize: 15,
                },
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    });

    // 绘制堆叠条形图
    let data1Arr = days.map(v => v.exerciseData[0].calorie);
    let data2Arr = days.map(v => v.exerciseData[1].calorie);
    let data3Arr = days.map(v => v.exerciseData[2].calorie);
    var myChart3 = echarts.init(dom.get('.chart3'));
    myChart3.setOption({
        title: {
            text: '近七日运动分类',
            top: 15,
            left: 20,
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                // Use axis to trigger tooltip
                type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
            }
        },
        legend: {
            orient: 'vertical',
            top: 10,
            right: 0,
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'value'
        },
        yAxis: {
            type: 'category',
            data: x1,
        },
        series: [
            {
                name: '跑步',
                type: 'bar',
                stack: 'total',
                label: {
                    show: true
                },
                emphasis: {
                    focus: 'series'
                },
                data: data1Arr
            },
            {
                name: '骑行',
                type: 'bar',
                stack: 'total',
                label: {
                    show: true
                },
                emphasis: {
                    focus: 'series'
                },
                data: data2Arr
            },
            {
                name: '训练',
                type: 'bar',
                stack: 'total',
                label: {
                    show: true
                },
                emphasis: {
                    focus: 'series'
                },
                data: data3Arr
            },
        ]
    });


    // 绘制条形图
    //处理数据
    let dateArr = days.map(v => v.count)
    var myChart4 = echarts.init(dom.get('.chart4'));
    myChart4.setOption({
        title: {
            text: '近七日训练次数',
            top: 15,
            left: 20,
        },
        xAxis: {
            type: 'category',
            data: x1
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: dateArr,
                type: 'line',
                color: ['#58bb92']
            }
        ]
    });
})